package c.mars.tower_of_hanoi.common;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;

public class Renderer implements GLSurfaceView.Renderer {

//	Common for all Renderers
	// mMVPMatrix is an abbreviation for "Model View Projection Matrix"
	protected final float[] mMVPMatrix = new float[16];
	protected final float[] mProjectionMatrix = new float[16];
	protected final float[] mViewMatrix = new float[16];
	protected final float[] mRotationMatrix = new float[16];	
	
	protected final static String TAG = "Renderer";
	
//	Standard overrides (will be implemented in derived classes)
	@Override
	public void onDrawFrame(GL10 gl) {
		
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
	}

//	Common for all Renderers
	public static void checkGlError(String glOperation) {
        int error;
        while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
            Log.e(TAG, glOperation + ": glError " + error);
            throw new RuntimeException(glOperation + ": glError " + error);
        }
    }
	
	public static int loadShader(int type, String shaderCode){

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }
}
